package com.zzyl.common.ai;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.*;

public class QianfanChatClient {

    private final RestTemplate restTemplate;
    private static final String API_URL = "https://qianfan.baidubce.com/v2/chat/completions";
    private static final String AUTH_TOKEN = "Bearer bce-v3/ALTAK-aalzI5zu6BQ88RhDlSA3t/8d923b008348917f2e86074bfe744956f295d92d ";

    public QianfanChatClient() {
        this.restTemplate = new RestTemplate();
    }

    private Map<String, String> createMessage(String role, String content) {
        Map<String, String> message = new HashMap<>();
        message.put("role", role);
        message.put("content", content);
        return message;
    }

    public String sendChatRequest() {
        try {
            // 创建请求体
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("model", "ernie-3.5-8k");

            List<Map<String, String>> messages = new ArrayList<>();
            messages.add(createMessage("system", "平台助手"));
            messages.add(createMessage("user", "王扁是谁,他和王源有什么关系吗"));

            requestBody.put("messages", messages);

            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.set("Authorization", AUTH_TOKEN);

            // 创建请求实体
            HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);

            // 发送POST请求
            ResponseEntity<String> response = restTemplate.exchange(
                    API_URL,
                    HttpMethod.POST,
                    requestEntity,
                    String.class
            );

            if (response.getStatusCode() == HttpStatus.OK) {
                String responseBody = response.getBody();
                return extractContentFromJson(responseBody);
            } else {
                return "请求失败，状态码: " + response.getStatusCode();
            }

        } catch (Exception e) {
            return "请求异常: " + e.getMessage();
        }
    }

    private String extractContentFromJson(String jsonResponse) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(jsonResponse);

            // 提取content内容
            JsonNode choicesNode = rootNode.path("choices");
            if (choicesNode.isArray() && choicesNode.size() > 0) {
                JsonNode firstChoice = choicesNode.get(0);
                JsonNode messageNode = firstChoice.path("message");
                JsonNode contentNode = messageNode.path("content");

                if (!contentNode.isMissingNode()) {
                    return contentNode.asText();
                }
            }
            return "未找到content内容";
        } catch (Exception e) {
            return "解析JSON失败: " + e.getMessage();
        }
    }

    // 添加main方法用于启动
    public static void main(String[] args) {
        QianfanChatClient client = new QianfanChatClient();
        String response = client.sendChatRequest();
        System.out.println( response);
    }
}